package util;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author kinde
 */
import java.util.Scanner;
import java.util.LinkedList;
import java.io.*;
import java.util.StringTokenizer;

public class TypeHandler {

    public void activeFilter(String ext, String path, String fileName, String command)
    {
        
        //String path = "\"" + pathLess + "\"";
        try{
            if(ext.equals("PDF"))
            {
                this.toFilter("/usr/local/mediawhere/filter/bin", "java -jar BinaryFilter.jar", path, fileName, command);
                
            }
            else if(ext.equals("DOC"))
            {
                this.toFilter("/usr/local/mediawhere/filter/bin", "java -jar BinaryFilter.jar", path, fileName, command);
            }
            else if(ext.equals("TXT"))
            {
                this.toFilter("/usr/local/mediawhere/filter/bin", "java -jar BinaryFilter.jar", path, fileName, command);
            }
            else if(ext.equals("ODT"))
            {
                this.toFilter("/usr/local/mediawhere/filter/bin", "java -jar BinaryFilter.jar", path, fileName, command);
            }
            else if(ext.equals("EPUB"))
            {
                this.toFilter("/usr/local/mediawhere/filter/bin", "java -jar BinaryFilter.jar", path, fileName, command);
            }
            else if(ext.equals("MP3"))
            {
                this.toFilter("/usr/local/mediawhere/filter/mp3", "java -jar Mp3Filter.jar", path, fileName, command);
            }
            else if(ext.equals("JPEG") || ext.equals("JPG"))
            {
                this.toFilter("/usr/local/mediawhere/filter/jpeg", "java -jar ImageFilter.jar", path, fileName, command);
            }
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    ///home/kinder/NetBeansProjects/BinaryFilter/dist/
    //java -jar BinaryFilter.jar
    private int toFilter(String homeDirFilter, String cmd, String path, String fileName, String typeOfCmd)
    {
        String finalPath = new String();
        String finalFileName = "\"" + fileName + "\"";
        StringTokenizer strTok = new StringTokenizer(path, "/");
        while(strTok.hasMoreTokens())//metto la parte finale del path(il nome del file) tra virgolette
        {
            String tok = strTok.nextToken();
            if(tok.equals(fileName))
                finalPath = finalPath + "/" + finalFileName;
            else
            finalPath = finalPath + "/\"" + tok + "\"";
        }


        File wd = new File(homeDirFilter);
        System.out.println(wd);
        Process proc = null;
        try {
           proc = Runtime.getRuntime().exec("/bin/bash", null, wd);
        }
        catch (IOException e) {
           e.printStackTrace();
           return 1;
        }
        if (proc != null) {
           BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
           PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
           out.println( cmd + " " + finalPath + " " + finalFileName + " " + typeOfCmd);
           out.println("exit");
           try {
              String line;
              while ((line = in.readLine()) != null) {
                 System.out.println(line);
              }
              proc.waitFor();
              in.close();
              out.close();
              proc.destroy();
           }
           catch (Exception e) {
              e.printStackTrace();
           }
        }
        return 0;
}

}
